namespace SimpleWebServices.Models
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;

    [XmlRoot("telephone")]
    public sealed class Telephone
    {
        [XmlAttribute("key")]
        public string Key
        {
            get;
            set;
        }

        [XmlAttribute("normalized")]
        public string Normalized
        {
            get;
            set;
        }

        [XmlAttribute("number")]
        public string Number
        {
            get;
            set;
        }

        public static string Normalize(int code, string value)
        {
            string result = value;

            if (!string.IsNullOrEmpty(value))
            {
                result = value.RemoveNonnumericCharacters();
                if (result.StartsWith("0", StringComparison.Ordinal))
                {
                    result = code.ToXmlString() + result.Substring(1);
                }

                result = "+" + result;
            }

            return result;
        }
    }
}